from rest_framework import serializers

from book.models import BookInfo



# class PeoplesSerializers(serializers.Serializer):
#     """书籍关联人物信息序列化器"""
#     id = serializers.IntegerField()
#     name = serializers.CharField()
#     description = serializers.CharField()
#
#     # 关联对象嵌套序列化
#     # book = serializers.PrimaryKeyRelatedField(queryset=BookInfo.objects.all())
#     book_id = serializers.IntegerField()
#     #
#     book = serializers.StringRelatedField()
#
#
#
#
# class BookInfoSerializers(serializers.Serializer):
#     """书籍信息序列化器"""
#     id = serializers.IntegerField()
#     name = serializers.CharField()
#     pub_date = serializers.DateField()
#     readcount = serializers.IntegerField()
#
#     people = PeoplesSerializers(many=True)
#
#
#
# class PeopleInfoSerializers(serializers.Serializer):
#     """人物信息序列化器"""
#     id = serializers.IntegerField()
#     name = serializers.CharField()
#     description = serializers.CharField()
#
#     # 关联对象嵌套序列化
#     # book = serializers.PrimaryKeyRelatedField(queryset=BookInfo.objects.all())
#     book_id = serializers.IntegerField()
#     #
#     book = serializers.StringRelatedField()


# *************************************************
# """反序列化"""
# class BookInfoSerializer(serializers.Serializer):
#     """图书数据序列化器"""
#     id = serializers.IntegerField(label='ID', read_only=True)
#     name = serializers.CharField(label='名称', max_length=20)
#     pub_date = serializers.DateField(label='发布日期', required=False)
#     readcount = serializers.IntegerField(label='阅读量', required=False)
#     commentcount = serializers.IntegerField(label='评论量', required=False)
#
#     def create(self, validated_data):
#         """保存数据"""
#         return BookInfo.objects.create(**validated_data)
#
#     def update(self, instance, validated_data):
#         """更新数据"""
#         # instance 是传入的要进行更新的数据对象
#         instance.name = validated_data.get('name', instance.name)
#         instance.pub_date = validated_data.get('pub_date', instance.pub_date)
#         instance.readcount = validated_data.get('readcount', instance.readcount)
#         instance.commentcount = validated_data.get('commentcount', instance.commentcount)
#         instance.save()
#         return instance
#
#
#
# class PeopleInfoSerializer(serializers.Serializer):
#
#     pass


# *****************************************************
"""模型类序列化器ModelSerializer"""
#
# class BookInfoModelSerializer(serializers.ModelSerializer):
#
#
#     class Meta:
#         model=BookInfo
#         # fields = '__all__'
#         # exclude = ('image',)
#         fields = ('id','name', 'readcount', 'commentcount')
#         # 序列化输出字段
#         read_only_fields = ('id', 'readcount', 'commentcount')
#
#         # 修改字段选项
#         extra_kwargs = {
#             'readcount': {'min_value': 0, 'required': True},
#             'commentcount': {'max_value': 0, 'required': True},
#         }

# *******************************************
"""进阶使用"""
from rest_framework import serializers
from book.models import PeopleInfo,BookInfo
class PeopleInfoModelSerializer(serializers.ModelSerializer):
    book_id = serializers.IntegerField(required=False)
    book = serializers.StringRelatedField()

    class Meta:
        model = PeopleInfo
        fields = ['id','book_id','book','name','password','is_delete']
        extra_kwargs= {
            'password':{'write_only':True},
            'is_delete':{'read_only':True}
        }



class BookInfoModelSerializer(serializers.ModelSerializer):
    # 一对多
    people = PeopleInfoModelSerializer(many=True)
    class Meta:
        model = BookInfo
        fields = '__all__'


    def create(self, validated_data):

        people = validated_data.pop('people')

        book = BookInfo.objects.create(**validated_data)


        for person in people:
            PeopleInfo.objects.create(book=book,**person)

        return book
